<template>
<div class="category">
  <!--显示所有的上架的父分类-->
  <ul>
    <li @click="SelectFirstCategory({})" :class="{active: !firstCategorySelected.id}">全部</li>
    <li
        v-for="(category, index) in firstCategoryList"
        :key="index"
        :class="{active: firstCategorySelected.id == category.id}"
        @click="SelectFirstCategory(category)">{{category.name}}</li>
  </ul>
  <!--显示父分类下的子分类-->
  <ul v-if="firstCategorySelected.id">
    <li @click="selectSecondCategory({})" :class="{active: !secondCategorySelected.id}">全部</li>
    <li
        v-for="(categoryChild, index) in secondCategoryList"
        :key="index"
        :class="{active: secondCategorySelected.id == categoryChild.id}"
        @click="selectSecondCategory(categoryChild)"
        >{{categoryChild.name}}</li>
  </ul>
</div>
  <!--显示搜索到的商品-->
  <div class="goodsList">
    <ul>
      <li v-for="(goods, index) in pageInfo.list" :key="index" @click="toGoodsView(goods.id)">
        <div class="pic">
          <el-image
              :src="`${SERVER_ADDR}/goods/pic/${goods.picList[0].url}`"
              style="width: 225px; height: 150px"
          />
        </div>
        <div class="name">{{ goods.name }}</div>
        <div class="dscp">{{ goods.dscp}}</div>
        <div class="price">￥ {{goods.price}}</div>
      </li>
    </ul>
  </div>

  <!-- 分页开始 -->
  <el-row class="row-bg" justify="center">
    <el-pagination background layout="prev, pager, next"
                   :page-count="pageInfo.pages"
                   @update:current-page="search"
                   style="margin-top: 10px;"
                   v-model:current-page="pageInfo.pageNum" />
  </el-row>
  <!-- 分页结束 -->
</template>

<script setup>
import {ref, watch} from "vue";
import categoryApi from "@/api/categoryApi.js";
import {Search} from "@element-plus/icons-vue";
import goodsApi from "@/api/goodsApi.js";
import {useRouter} from "vue-router";
import {useRoute} from "vue-router";

const route = useRoute()
const router = useRouter()
//服务器的地址
const SERVER_ADDR = ref(import.meta.env.VITE_SERVER_ADDR)
//获取通过路径传递的参数
const props = defineProps({
  categoryId: String
});
//所以需要显示的父分类
const firstCategoryList = ref([]);
//所有需要的子分类
const  secondCategoryList = ref([]);
// 被选中的父分类
const firstCategorySelected = ref({});
// 被选中的子分类
const  secondCategorySelected = ref({});
//被选中的分类id 可以是父分类 也可以是子分类
const categoryId = ref(null);
//分页的信息
const pageInfo = ref({
  pages: 0,
  pageSize: 0,
  pageNum: 0
});
//通过路径传递的参数发生变化进行的处理
watch(props, () => {
  categoryId.value = props.categoryId;
  console.log(props)
  getParent();
  //查询商品
  search(1);
})
//监听goodsName参数
watch(() => route.query.goodsName, () => {
  search(1);
});
//点击父分类调用的函数
const SelectFirstCategory = (category) => {
  firstCategorySelected.value = category;
  //只要选择某个父分类就要清空所有的子分类
  secondCategoryList.value = ([]);
  //给被选中的分类id
  if (firstCategorySelected.value.id){
    categoryId.value = firstCategorySelected.value.id;
  } else {
    categoryId.value = null;
  }

  //给被选中的父分类下的子分类赋值
  if (category.childList) {
    category.childList
        .filter(c => c.status == 1)
        .forEach(c => secondCategoryList.value.push(c))
  }
  //查询商品
  search(1);
}
//点击父分类调用的函数
const selectSecondCategory = (category) => {
  secondCategorySelected.value = category;
  //给被选中的分类id赋值
  if (secondCategorySelected.value.id){
    categoryId.value = secondCategorySelected.value.id;
  }else {
    categoryId.value = firstCategorySelected.value.id;
  }
  //查询商品
  search(1);
}
//1.获取父分类 2.获取上架的 3.推荐的
const getParent = ()=>{
  const condition = {
    parentId: 0,
    status: 1,
    recom: 1,
  };
  categoryApi.selectByPage(condition)
      .then(resp => {
        firstCategoryList.value = resp.data;
        //假设没有通过路径传递的参数或者传递id在父分类中不存在
        let float = false;
        firstCategoryList.value.forEach(c => {
          if (props.categoryId == c.id) {
            SelectFirstCategory(c)
            float = true;
          }
        })
        if (!float){
          SelectFirstCategory({})
        }
      })
}
//搜索
const search = (pageNum) => {
  const condition = {
    status: 1,
    categoryId: categoryId.value,
    //接收goodsName
    name: route.query.goodsName
  }

  goodsApi.selectByPage1(condition, pageNum, 20)
      .then(resp => {
        pageInfo.value = resp.data;
      })
}
//跳转到商品详情页
const toGoodsView = (id)=>{
  router.push({
    path: '/user/goods',
    query: {
      id
    }
  });

}
search(1);
getParent();
</script>

<style scoped>
  .category ul li {
    float: left;
    margin: 10px 20px;
    font-size: 14px;
    cursor: pointer;
  }

  .active {
    color: var(--theme-color);
    font-weight: bold;
  }
  .goodsList ul li {
    width: 224px;
    float: left;
    margin-right: 20px;
    margin-bottom: 15px;
    text-align: center;
    background-color: #fff;;
    cursor: pointer;

  }
  .goodsList ul li:hover{
    box-shadow: 0px 0px 20px #000;
  }
  .goodsList ul li:nth-child(5n) {
    margin-right: 0px;
  }
  .name, .dscp, .price{
    line-height: 25px;
  }
  .name {
    font-size: 16px;
    font-weight: bold;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .dscp {
    color: #aaa;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .price {
    font-size: 12px;
    color:  var(--theme-color);
  }
</style>